/**
 * @ClassName: UserServiceImpl
 * @Description
 * @Author maozhi
 * @Date 2021/3/22
 * @Time 14:56
 */

package com.universityJob.modules.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.universityJob.common.utils.R;
import com.universityJob.modules.app.service.UserService;
import com.universityJob.common.exception.RRException;
import com.universityJob.common.validator.Assert;
import com.universityJob.modules.app.dao.UserDao;
import com.universityJob.modules.app.entity.UserEntity;
import com.universityJob.modules.app.form.LoginForm;
import com.universityJob.modules.data.dao.DataHistoryDao;
import com.universityJob.modules.data.dao.DataJobDao;
import com.universityJob.modules.data.dao.DataRecDao;
import com.universityJob.modules.data.entity.DataHistoryEntity;
import com.universityJob.modules.data.entity.DataJobEntity;
import com.universityJob.modules.data.entity.DataRecEntity;
import com.universityJob.modules.data.entity.Recommend;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private DataJobDao dataJobDao;
    @Autowired
    private DataRecDao dataRecDao;
    @Autowired
    private DataHistoryDao dataHistoryDao;

    @Override
    public UserEntity queryByMobile(String mobile) {
        return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
    }

    @Override
    public String login(LoginForm form) {
        UserEntity user = queryByMobile(form.getUsername());
        Assert.isNull(user, "学号或密码错误");

        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))) {
            throw new RRException("手机号或密码错误");
        }

        return user.getUserId();
    }

    @Override
    public Page<DataRecEntity> getRecList(Map rec) {
        Page<DataRecEntity> page = new Page<>(MapUtils.getIntValue(rec, "page", 1), MapUtils.getIntValue(rec, "limit", 15));
		try {
			rec.remove("page");
		} catch (Exception e) {
		}
		try {
			rec.remove("limit");
		} catch (Exception e) {
		}
        page.setOptimizeCountSql(false);
        List<DataRecEntity> dataRecEntities = dataRecDao.selectReclist(page, rec);
        return page.setRecords(dataRecEntities);
    }

    @Override
    public R getJobList(Map job) {
        String pay = MapUtils.getString(job, "pay");
        if (pay != null) {
            String[] split = pay.split("-");
            String low = "";
            String max = "";
            if (split.length > 0) {
                max = split[0];
                low = split[1];
                job.put("highmonthpay", max);
                job.put("lowmonthpay", low);
            }
        }
		Page<DataJobEntity> page = new Page<>(MapUtils.getIntValue(job, "page", 1), MapUtils.getIntValue(job, "limit", 15));
		try {
			job.remove("page");
		} catch (Exception e) {
		}
		try {
			job.remove("limit");
		} catch (Exception e) {
		}
        page.setOptimizeCountSql(false);
        List<DataJobEntity> list = dataJobDao.selectJobList(page, job);
        return R.ok().put("page", page.setRecords(list));
    }

    @Override
    public R getOneJob(String id) {
        DataJobEntity dataJobEntity;
        try {
            dataJobEntity = dataJobDao.selectJobByid(id.toString());
        } catch (Exception e) {
            return R.error("未找到数据");
        }
        return R.ok().put("job", dataJobEntity);
    }

    @Override
    public R getOneRec(String id) {
        DataRecEntity dataRecEntity;
        try {
            dataRecEntity = dataRecDao.selectById(id);
        } catch (Exception e) {
            return R.error("未找到数据");
        }
        return R.ok().put("rec", dataRecEntity);
    }

    @Override
    public R getHistoryList(Map stu) {
        Page<DataHistoryEntity> page = new Page<>(MapUtils.getIntValue(stu, "page", 1), MapUtils.getIntValue(stu, "limit", 15));
        try {
            stu.remove("page");
        } catch (Exception e) {
        }
        try {
            stu.remove("limit");
        } catch (Exception e) {
        }
        page.setOptimizeCountSql(false);
        List<DataHistoryEntity> dataHistoryEntities = dataHistoryDao.selectAll(page,stu);
        return R.ok().put("page", page.setRecords(dataHistoryEntities));
    }

    @Override
    public R getAllOlympiad() {
        List allOlympiad = dataHistoryDao.getAllOlympiad();
        return R.ok().put("olympiad", allOlympiad);
    }

    @Override
    public R findRecommends(Map<String, Object> params) {
        Page<Map> page = new Page<>(MapUtils.getIntValue(params, "page", 1), MapUtils.getIntValue(params, "limit", 15));
        try {
            params.remove("page");
        } catch (Exception e) {
        }
        try {
            params.remove("limit");
        } catch (Exception e) {
        }
        page.setOptimizeCountSql(false);
        List<Map> recommends = dataHistoryDao.findRecommends(page,params);
        return  R.ok().put("page", page.setRecords(recommends));
    }

    @Override
    public R upRecommendsStatus(String id) {
        int i = dataHistoryDao.upRecommendsStatus(id);
        if (i > 0){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
